﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webapi.Models;
using Webdiyer.WebControls.Mvc;

namespace Webapi.Controllers
{
    [Authorize]
    public class ControlController : Controller
    {
        public ActionResult Index(int id = 0)
        {
            using (EstateLicenceEntities db = new EstateLicenceEntities())
            {
                var list = db.LicenceCode.OrderByDescending(o => o.L_Time).ToPagedList(id, 15);

                return View(list);
            }
        }

        /// <summary>
        /// 删除密钥
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Remove(Guid? id)
        {
            if (id.HasValue)
            {
                using (EstateLicenceEntities db = new EstateLicenceEntities())
                {
                    var obj = db.LicenceCode.FirstOrDefault(o => o.L_Id == id);
                    obj.L_State = 1;
                    db.SaveChanges();
                }
            }
            return RedirectToAction("Index");
        }

        /// <summary>
        /// 编辑密钥
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(Guid? id)
        {
            LicenceCode obj = null;
            if (id.HasValue)
            {
                using (EstateLicenceEntities db = new EstateLicenceEntities())
                {
                    obj = db.LicenceCode.FirstOrDefault(o => o.L_Id == id);
                }
            }
            else
            {
                obj = new LicenceCode()
                {
                    L_Id = Guid.Empty,
                    L_Time = DateTime.Now,
                    L_TimeUseful = DateTime.Now.AddMonths(2)
                };
            }
            return View(obj);
        }
        /// <summary>
        /// 编辑项目密钥
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(LicenceCode obj)
        {
            if (ModelState.IsValid)
            {
                obj.L_Time = DateTime.Now;
                using (EstateLicenceEntities db = new EstateLicenceEntities())
                {
                    if (obj.L_Id == Guid.Empty)
                    {
                        obj.L_Id = Guid.NewGuid();
                        db.LicenceCode.Add(obj);
                    }
                    else
                    {
                        db.Set<LicenceCode>().Attach(obj);
                        db.Entry(obj).State = EntityState.Modified;
                    }

                    if (db.SaveChanges() < 1)
                    {
                        return View(obj);
                    }
                }

            }
            return RedirectToAction("Index");
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <returns></returns>
        public ActionResult Pwd()
        {
            string uname = ConfigHelper.GetValue("username");
            ViewBag.Uname = uname;
            return View();
        }
        [HttpPost]
        public ActionResult Pwd(FormCollection obj)
        {
            string newName = obj["uname"];
            string newPwd= obj["pwd"];
            if (!string.IsNullOrEmpty(newName)&&!string.IsNullOrEmpty(newPwd))
            {

                ConfigHelper.SetValue("username",newName);

                ConfigHelper.SetValue("password",System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(newPwd, "MD5"));
            }
            ViewBag.Uname = newName;
            ViewBag.Tips= "变更成功！";
            return View();
        }
    }
}
